package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.persistence.dao.TOrderMapper;
import com.stylefeng.guns.common.persistence.dao.UserMapper;
import com.stylefeng.guns.common.persistence.model.TOrder;
import com.stylefeng.guns.common.persistence.model.TOrderFee;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.business.service.IDotService;
import com.stylefeng.guns.modular.system.service.ITOrderFeeService;
import com.stylefeng.guns.modular.system.service.ITOrderService;
import com.stylefeng.guns.modular.system.warpper.OrderFeeWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收银中心控制器
 *
 * @author fengshuonan
 * @Date 2018-01-28 17:26:47
 */
@Controller
@RequestMapping("/tOrderFeeOff")
public class TOrderFeeOffController extends BaseController {

    private String PREFIX = "/system/tOrderOffFee/";

    @Autowired
    private ITOrderFeeService tOrderFeeService;
    @Autowired
    private ITOrderService itOrderService;

    @Autowired
    private IDotService iDotService;

    @Autowired
    UserMapper userMapper;
    @Autowired
    TOrderMapper orderMapper;
    /**
     * 跳转到收银中心首页
     */
    @RequestMapping("")
    public String index(Model model) {
        List<Map<String, Object>> fees = itOrderService.getFee(ShiroKit.getUser().getDeptId() + "");
        List<Map<String, Object>> feess = tOrderFeeService.getFee(ShiroKit.getUser().getDeptId() + "");
        for(Map<String,Object> map:feess){
            fees.add(map);
        }
        model.addAttribute("fees", fees);

//        model.addAttribute("fees", fees);

        return PREFIX + "tOrderFee.html";
    }

    /**
     * 跳转到添加收银中心
     */
    @RequestMapping("/tOrderFee_add")
    public String tOrderFeeAdd(Model model) {
//        List<Map<String, Object>> fees = itOrderService.getFee(ShiroKit.getUser().getDeptId() + "");
        List<Map<String, Object>> feess = tOrderFeeService.getFee(ShiroKit.getUser().getDeptId() + "");
//        for(int i=0;i<fees.size();i++){
//            if(fees.get(i).get("fee_type").equals("0")){
//                fees.remove(i);
//            }
//        }
        model.addAttribute("fees", feess);
        return PREFIX + "tOrderFee_add.html";
    }

    /**
     * 跳转到修改收银中心
     */
    @RequestMapping("/tOrderFee_update")
    public String tOrderFeeUpdate(String ids, Model model) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> maps = tOrderFeeService.selectFeeList(list);
        model.addAttribute("ids", ids);
//        model.addAttribute("count",list.size());

        float sum = 0;
        int mA = 0;
        for (int i = 0; i < maps.size(); i++) {
//            if(maps.get(i).get("payments_type").equals("2")){
//                sum=sum-Float.valueOf(maps.get(i).get("money").toString());
//            }else{
            sum = sum + Float.valueOf(maps.get(i).get("money").toString());
//            }
            String settlement_type=maps.get(i).get("settlement_type")==null?"0":maps.get(i).get("settlement_type").toString();
            if (settlement_type.equals("1")) {
                mA = +1;
            }
        }
        model.addAttribute("allcount", list.size());
        model.addAttribute("money", sum);
        model.addAttribute("mA", mA);
        return PREFIX + "tOrderFee_edit.html";
    }

    @RequestMapping(value = "/listFee")
    @ResponseBody
    public Object listFee(String ids) {
        Page<Map<String, Object>> page = new Page(0, 0);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> maps = tOrderFeeService.selectFeeList(list);
        pageInfoBT.setData(maps);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }


    /**
     * 获取收银中心列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, Integer offset, Integer limit,
                       @RequestParam(required = false) String dj_type, @RequestParam(required = false) String feeType
            , @RequestParam(required = false) String auditStatus, @RequestParam(required = false) String beginTime
            , @RequestParam(required = false) String endTime , @RequestParam(required = false) String pname,
                       @RequestParam(required = false) String payMode
    ) {
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        Map m = new HashMap();
        int userid=ShiroKit.getUser().getId();
        int dot_id=userMapper.selectById(userid).getDot_id();
        m.put("name", name);
        m.put("dj_type", dj_type);
        m.put("feeType", feeType);
        m.put("auditStatus", auditStatus);
        m.put("beginTime", beginTime);
        m.put("endTime", endTime);
        m.put("dot_id", dot_id);
        m.put("pname", pname);
        m.put("payMode", payMode);
        if (ShiroKit.isAdmin()) {
            m.put("deptid", "");
        } else {
            m.put("deptid", ShiroKit.getUser().getDeptId() + "");
        }

        List<Map<String, Object>> list = tOrderFeeService.selectOrderFeeOffList(page, m);
        new OrderFeeWarpper(list).warp();
        pageInfoBT.setData(list);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    @RequestMapping(value = "/listSum")
    @ResponseBody
    public Object listSum(@RequestParam(required = false) String name, Integer offset, Integer limit,
                          @RequestParam(required = false) String dj_type, @RequestParam(required = false) String feeType
            , @RequestParam(required = false) String auditStatus, @RequestParam(required = false) String beginTime
            , @RequestParam(required = false) String endTime , @RequestParam(required = false) String pname
    ) {
        Map m = new HashMap();
        int userid = ShiroKit.getUser().getId();
        int dot_id = userMapper.selectById(userid).getDot_id();
        m.put("name", name);
        m.put("dj_type", dj_type);
        m.put("feeType", feeType);
        m.put("auditStatus", auditStatus);
        m.put("beginTime", beginTime);
        m.put("endTime", endTime);
        m.put("pname", pname);
        m.put("dot_id", dot_id);
        if (ShiroKit.isAdmin()) {
            m.put("deptid", "");
        } else {
            m.put("deptid", ShiroKit.getUser().getDeptId() + "");
        }

        List<Map<String, Object>> list = tOrderFeeService.selectOrderFeeOffSum(m);
        return list;
    }

    /**
     * 新增收银中心
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TOrderFee tOrderFee) {
        int dot_id = userMapper.selectById(ShiroKit.getUser().getId()).getDot_id();
        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
        tOrderFee.setDotId(dot_id);
        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
//        tOrderFee.setAuditStatus(1);
        tOrderFee.setFeeIncome(1);
        tOrderFee.setSettlementType("0");
        tOrderFee.setPayMode(0);
        tOrderFeeService.insert(tOrderFee);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除收银中心
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tOrderFeeId) {
        tOrderFeeService.deleteById(tOrderFeeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改收银中心
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(String ids, String pay_mode, int status) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            TOrderFee tOrderFee = new TOrderFee();
            tOrderFee.setId(Integer.valueOf(ids.split(",")[i]));
            tOrderFee.setAuditStatus(status);
            if(!pay_mode.equals("-1")){
                tOrderFee.setPayMode(Integer.valueOf(pay_mode));
            }
            tOrderFeeService.updateById(tOrderFee);
            if(status==3){
                String order_id=tOrderFeeService.selectById(Integer.valueOf(ids.split(",")[i])).getOrderId()+"";
                Map map=orderMapper.selectMinOrderStatus(order_id);
                if(map.get("audit_status").toString().equals("3")){
                    Map map1=orderMapper.selectOrderReceiptStatus(order_id);
                    if(map1.get("receipt_status").toString().equals("1")){
                        TOrder order=new TOrder();
                        order.setId(Integer.valueOf(order_id));
                        order.setStatus(3);
                        orderMapper.updateById(order);
                    }
                }
            }
        }
        return super.SUCCESS_TIP;
    }

    /**
     * 收银中心详情
     */
    @RequestMapping(value = "/detail/{tOrderFeeId}")
    @ResponseBody
    public Object detail(@PathVariable("tOrderFeeId") Integer tOrderFeeId) {
        return tOrderFeeService.selectById(tOrderFeeId);
    }
}
